﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Dotway.WPF.Controls.Utilities;

namespace Dotway.WPF.Controls
{
    public class ColorPicker : Control
    {
        public ColorPicker()
        {
            this.Loaded += new RoutedEventHandler(OnColorPickerLoaded);
        }        

        static ColorPicker()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPicker), new FrameworkPropertyMetadata(typeof(ColorPicker)));
        }

        #region Properties

        public static readonly DependencyProperty InitialColorProperty = DependencyProperty.Register("InitialColor", typeof(Color), typeof(ColorPicker), new UIPropertyMetadata(Color.FromRgb(0,0,0), new PropertyChangedCallback(OnInitialColorChanged)));
        public Color InitialColor
        {
            get { return (Color)GetValue(InitialColorProperty); }
            set { SetValue(InitialColorProperty, value); }
        }

        #region OnInitialColorChanged

        private static void OnInitialColorChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ColorPicker control = o as ColorPicker;
            if (control != null)
            {
                control.OnInitialColorChanged((Color)e.OldValue, (Color)e.NewValue);
            }
        }

        protected virtual void OnInitialColorChanged(Color oldValue, Color newValue)
        {
            SelectedColor.Color = newValue;
        }

        #endregion OnInitialColorChanged                
        
         

        public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register("SelectedColor", typeof(NotifiedColor), typeof(ColorPicker), new UIPropertyMetadata(new NotifiedColor()));
        public NotifiedColor SelectedColor
        {
            get { return (NotifiedColor)GetValue(SelectedColorProperty); }
            private set { SetValue(SelectedColorProperty, value); }
        }

        #endregion Properties

        private void OnColorPickerLoaded(object sender, RoutedEventArgs e)
        {
            SelectedColor.Color = InitialColor;
        }
    }
}
